package com.atlassian.jira.license;

import com.atlassian.gzipfilter.org.apache.commons.lang.StringUtils;
import com.atlassian.jira.config.properties.ApplicationProperties;
import com.atlassian.jira.util.dbc.Assertions;

public class JiraLicenseStoreImpl implements JiraLicenseStore {
	private static final String ENTERPRISE = "enterprise";
	private final ApplicationProperties applicationProperties;
	private final LicenseStringFactory licenseStringFactory;

	public JiraLicenseStoreImpl(ApplicationProperties applicationProperties,
			LicenseStringFactory licenseStringFactory) {
		this.licenseStringFactory = ((LicenseStringFactory) Assertions.notNull(
				"licenseStringFactory", licenseStringFactory));
		this.applicationProperties = ((ApplicationProperties) Assertions
				.notNull("applicationProperties", applicationProperties));
	}

	public String retrieve() {
		String licenseString = this.applicationProperties.getText("License20");
		if (StringUtils.isBlank(licenseString)) {
			licenseString = retreiveFromOldKeys();
		}
		if (StringUtils.isBlank(licenseString)) {
			licenseString = retriveFromReallyOldKeys();
		}
		if (StringUtils.isBlank(licenseString)) {
			licenseString = "AAABQQ0ODAoPeNpdkM1uwjAQhO9+Cks9B+WvrUCyVEoslIoElIS2h16WsBSXxIlsh5Y+PSaAiip5D\n"
					+ "9bszLe7d0kjaYQlDTzquaPQH3kBneQF9V3PJxHqUonWiEaylzgb2xa61Kj0x4jmBpRBRWeiRKmRp\n"
					+ "F29QjXf9DrzXDJRCCdnBAaZjQsc17OPTBppoDQp1MimTbWhfPcNxoCEnTDkSygYXDL5WvRonhY8W\n"
					+ "2Rxzq9mnoCoWCt0Z3D3tG1Mbf+DsqkJ30PV9Vy2gcrOdRtYHFrsuXkxzmzmWbR5Yo/MqA5JdW58t\n"
					+ "TucInxiQdKgBFki/2mFOlzXCS/rzNUnSKHPyHglfknOU2bL8cP7h9AbkhzVHlUcsedh+O48jpep8\n"
					+ "zadBU4SJpxcJrPqLI5uXdcb/EE7WYlaGFyTRafKLWj8f9ojuZ2XyTAsAhQ4ussWDekG0mqT5Syl+\n"
					+ "kDkKIIU0gIUH30RK8fUZmXzwjyjYPBijJIn4tE=X02g0";
		}
		return licenseString;
	}

	private String retreiveFromOldKeys() {
		return retrieveFromMessageAndHash(
				this.applicationProperties.getText("License Message Text"),
				this.applicationProperties.getText("License Hash 1 Text"));
	}

	private String retriveFromReallyOldKeys() {
		return retrieveFromMessageAndHash(
				this.applicationProperties.getString("License Message"),
				this.applicationProperties.getString("License Hash 1"));
	}

	private String retrieveFromMessageAndHash(String msg, String hash) {
		if ((StringUtils.isNotBlank(msg)) && (StringUtils.isNotBlank(hash))) {
			return this.licenseStringFactory.create(msg, hash);
		}

		return null;
	}

	public void store(String licenseString) {
		if (StringUtils.isBlank(licenseString)) {
			throw new IllegalArgumentException(
					"You can't store an empty license");
		}

		this.applicationProperties.setText("License20",
				removeWhitespace(licenseString));

		this.applicationProperties.setString("jira.edition", "enterprise");
	}

	public void remove() {
		this.applicationProperties.setText("License20", null);
		this.applicationProperties.setText("License Message Text", null);
		this.applicationProperties.setText("License Hash 1 Text", null);
		this.applicationProperties.setString("License Message", null);
		this.applicationProperties.setString("License Hash 1", null);
	}

	private String removeWhitespace(String licenseString) {
		if (StringUtils.isBlank(licenseString)) {
			return licenseString;
		}
		StringBuilder sb = new StringBuilder();
		for (char ch : licenseString.toCharArray()) {
			if (Character.isWhitespace(ch))
				continue;
			sb.append(ch);
		}

		return sb.toString();
	}

	public void resetOldBuildConfirmation() {
		this.applicationProperties.setOption(
				"jira.install.oldlicense.confirmed", false);
		this.applicationProperties.setString(
				"jira.install.oldlicense.confirmed.timestamp", "");
		this.applicationProperties.setString(
				"jira.install.oldlicense.confirmed.user", "");
	}

	public void confirmProceedUnderEvaluationTerms(String userName) {
		this.applicationProperties.setOption(
				"jira.install.oldlicense.confirmed", true);
		this.applicationProperties.setString(
				"jira.install.oldlicense.confirmed.user", userName);
		this.applicationProperties.setString(
				"jira.install.oldlicense.confirmed.timestamp",
				String.valueOf(System.currentTimeMillis()));
	}

	public String retrieveServerId() {
		return "B94X-7AUN-WGL3-M4ME";
//		return this.applicationProperties.getString("jira.sid.key");
	}

	public void storeServerId(String serverId) {
		this.applicationProperties.setString("jira.sid.key", serverId);
	}
}